package com.ebelter.btlibrary.util;

import android.text.TextUtils;

import org.apache.log4j.Logger;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 日志输出类
 */
public class ULog {

    /**
     * 正式上线是设为false
     */
    public static boolean isConfigured = false;
    /**
     * Log级别,规范Log打印环境
     **/
    public static final int MODE_VERBOSE = 1;
    public static final int MODE_DEBUG = 2;
    public static final int MODE_INFO = 3;
    public static final int MODE_WARM = 4;
    public static final int MODE_ERROR = 5;
    public static final int MODE_PRODUCT = 6;
    /**
     * 决定Log是否打印，默认为MODE_DEBUG模式，供开发人员使用，发版前将根据AppConfig，指定对应的Log级别
     **/
    private static int logMode = MODE_INFO;
    //	开启单线程线程池线程输出Log
    private static ExecutorService logService = Executors.newSingleThreadExecutor();

    //	不允许实例化
    private ULog() {
    }

    public static void setLogMode(int mode) {

      //  logMode = mode;
    }


    public static void v(final String tag, final String msg) {
        if (logMode <= MODE_VERBOSE) {
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.debug(msg);
                }
            });

        }
    }

    public static void d(final String tag, final String msg) {
        if (logMode <= MODE_DEBUG) {
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.debug(msg);
                }
            });
        }
    }

    public static void i(final String tag, final String msg) {



        if (logMode <= MODE_INFO) {
            final String msgTemp = Thread.currentThread().getId()+"###"+msg;
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.info(msgTemp);
                }
            });
        }

    }

    public static void w(final String tag, final String msg) {
        if (logMode <= MODE_WARM) {
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.warn(msg);
                }
            });
        }
    }

    public static void e(final String tag, final String msg) {
        if (logMode <= MODE_ERROR) {
            logService.execute(new Runnable() {
                @Override
                public void run() {
                    Logger LOGGER = getLogger(tag);
                    LOGGER.error(msg);
                }
            });
        }
    }

    private static Logger getLogger(String tag) {
        if (!isConfigured) {
            Log4jConfigure.configure();
            isConfigured = true;
        }
        Logger logger;
        if (TextUtils.isEmpty(tag)) {
            logger = Logger.getRootLogger();
        } else {
            logger = Logger.getLogger(tag);
        }
        return logger;
    }
}